package servlet;

import com.fasterxml.jackson.databind.ObjectMapper;
import dao.AppointmentsDao;
import dao.impl.AppointmentsDaoImpl;
import entity.Appointments;
import entity.RescheduleNotifications;
import entity.vo.AppointmentsData;
import utils.GetFieldUtils;
import utils.ResponseResult;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet(name = "AppointmentsServlet", urlPatterns = "/bigProjects/AppointmentsServlet")

public class AppointmentsServlet extends HttpServlet {


    // 预约挂号 需要参数: userId, doctorsId, datetime
    @Override
    protected void doPut(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        AppointmentsDao appointmentsDao = new AppointmentsDaoImpl();
        resp.setContentType("application/json");
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
        String str = GetFieldUtils.getRequestPayload(req);
        ObjectMapper mapper = new ObjectMapper();
        Appointments appointments = mapper.readValue(str, Appointments.class);

        int b = appointmentsDao.booking(appointments);
        if (b > 0) resp.getWriter().write(ResponseResult.success().toString());
        else resp.getWriter().write(ResponseResult.failure("预约失败").toString());
    }


    //用户查询所有预约过的记录，需要参数: userId
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        AppointmentsDao appointmentsDao = new AppointmentsDaoImpl();
        resp.setContentType("application/json");
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
        Integer userId = Integer.valueOf(req.getParameter("userId"));

        List<AppointmentsData> appointmentsData = appointmentsDao.queryByUserId(userId);
        resp.getWriter().write(ResponseResult.success(appointmentsData).toString());
    }

    //修改预约时间;需要 oldDate;   appointmentId;  newDate(前两条可以从页面获取，newDate需要用户输入)
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        AppointmentsDao appointmentsDao = new AppointmentsDaoImpl();
        resp.setContentType("application/json");
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
        String str = GetFieldUtils.getRequestPayload(req);
        ObjectMapper mapper = new ObjectMapper();
        RescheduleNotifications rescheduleNotifications = mapper.readValue(str, RescheduleNotifications.class);
        boolean b = appointmentsDao.updateTime(rescheduleNotifications);
        if (b) resp.getWriter().write(ResponseResult.success().toString());
        else resp.getWriter().write(ResponseResult.failure("修改失败").toString());
    }

    //取消预约;需要一个int类型的id
    @Override
    protected void doDelete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        AppointmentsDao appointmentsDao = new AppointmentsDaoImpl();
        resp.setContentType("application/json");
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
        Integer id = Integer.valueOf(req.getParameter("id"));
        int b = appointmentsDao.cancelById(id);
        if (b > 1) resp.getWriter().write(ResponseResult.success().toString());
        else resp.getWriter().write(ResponseResult.failure("取消失败").toString());
    }
}
